package com.adjaisd.scarecrow.common.config.job.jobs;

import com.adjaisd.scarecrow.common.utils.mqtt.EmqClient;
import com.adjaisd.scarecrow.entity.Device;
import com.adjaisd.scarecrow.entity.DeviceStatus;
import com.adjaisd.scarecrow.entity.Quota;
import com.adjaisd.scarecrow.service.DeviceService;
import com.adjaisd.scarecrow.service.DeviceStatusService;
import com.adjaisd.scarecrow.service.QuotaService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MockDataJob implements Job {

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceStatusService deviceStatusService;

    @Autowired
    QuotaService quotaService;

    @Autowired
    EmqClient emqClient;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        mockData();
    }

    private void mockData() {
        System.out.println(LocalDateTime.now() + "模拟数据产生");
        // 查询所有存在且开启的设备
        QueryWrapper<DeviceStatus> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DeviceStatus::getStatus, true);
        List<DeviceStatus> list = deviceStatusService.list(wrapper);
        List<Long> collect = list.stream().map(DeviceStatus::getId).collect(Collectors.toList());

        QueryWrapper<Device> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().gt(Device::getBindTime, 0L)
                .in(Device::getId, collect);

        List<Device> deviceList = deviceService.list(wrapper1);
        deviceList.forEach(device -> {
            // 查询所有指标列表
            QueryWrapper<Quota> wrapper2 = new QueryWrapper<>();
            wrapper2.lambda().gt(Quota::getCreateTime, 0L)
                    .eq(Quota::getNumber, device.getNumber());
            List<Quota> quotaList = quotaService.list(wrapper2);
            for (Quota quota : quotaList) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("number", device.getNumber());
                Random random = new Random();
                int quotaValue = 0;//产生随机数
                if (quota.getName().equals("温度"))
                    quotaValue += random.nextInt(50) - 10;
                else if (quota.getName().equals("湿度"))
                    quotaValue += random.nextInt(100);
                else quotaValue += random.nextInt(100);
                map.put(quota.getValueKey(), quotaValue);
                String json = JSON.toJSONString(map);
                emqClient.publish(quota.getSubject(), json, 1);
                try {
                    Thread.sleep(20 + random.nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
